package com.example.myapplication;

import android.util.Log;

import com.example.myapplication.db.User;
import com.speedystone.greendaodemo.db.DaoSession;
import com.speedystone.greendaodemo.db.UserDao;

import java.util.List;

/**
 * Created by Lx on 2018/8/10.
 */

public class Utils {
    //从后到前的分页查询,是否还有数据的标志,默认是还有数据
    static boolean isMoreRecord = true;
    private static UserDao userDao;


    List<User> getList(int page, int indexs) {
        int content = this.getContent();
        int offset = content - 5 * page;
        int limit = 5;
        int index = content - 5;
        if (index <= 0) {
            index = 0;
        }
        if (offset < 0) {
            isMoreRecord = false;
            limit = 5 + offset;
            if (limit <= 0) {

                return null;
            } else {
                return userDao.queryBuilder().where(UserDao.Properties.Age.eq(10)).offset(indexs).limit(limit).list();
            }
        } else if (offset > 0) {

            return userDao.queryBuilder().where(UserDao.Properties.Age.eq(10)).offset(indexs).limit(5).list();

        } else if (offset == 0) {
            isMoreRecord = false;
            return userDao.queryBuilder().where(UserDao.Properties.Age.eq(10)).offset(indexs).limit(5).list();
        }

        return null;
    }

    int getContent() {

        return userDao.queryBuilder().where(UserDao.Properties.Age.eq(10)).list().size();
    }

    void getListHolder(int page) {
        //下标
        int index = this.getContent() - 10;
        //每次获取的数量
        int limit = 10;
        //获取总数
        int content = this.getContent();
        //当前页码乘要获取的数据量
        int number = limit * page;

        int offset = content - limit * page;

        //说明没有那么多数据
        if (offset < 0) {
            //判断
            limit = Math.abs(number - content);

        } else if (number == content) {
            isMoreRecord = false;
        }


    }

    private static class UtilsHolder {
        private static final Utils utils = new Utils();

    }

    static Utils getInstance() {

        return UtilsHolder.utils;
    }

    Utils() {
        DaoSession daoSession = MyApp.getDaoSession();
        userDao = daoSession.getUserDao();
    }
}